﻿using System;
using System.Data;
using System.Configuration;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;

namespace Dasboard.DatePicker
{
    public class DateSelector : WebControl
    {
        public System.Web.UI.WebControls.TextBox txt_Date;
        protected System.Web.UI.WebControls.Image imgCalendar;        

        public DateSelector(int count)
        {
            // Create Instance of webcontrols
            txt_Date = new TextBox();
            txt_Date.ID = "txtDate"+ count;
            txt_Date.EnableViewState = true;
            imgCalendar = new System.Web.UI.WebControls.Image();
            imgCalendar.ImageUrl = "DatePicker/cal/calendar.gif";

            Controls.Add(buildComponents());   
        }

        private Table buildComponents()
        {
            // Create Tables
            Table table = new Table();
            TableRow row = new TableRow();
            table.Rows.Add(row);
            TableCell celBox = new TableCell();
            // Put the textbox in a cell
            celBox.Controls.Add(txt_Date);
            row.Cells.Add(celBox);
            TableCell celImg = new TableCell();
            // Put the image in the cell
            celImg.Controls.Add(imgCalendar);
            row.Cells.Add(celImg);

            // Return the table with textbox and image for displaying.
            return table;
        }

        public void addSetPopup(string id)
        { 
            // Add the onclick code for the popup
            string scriptStr = "javascript:return popUpCalendar(this," + id + @", 'mm/dd/yyyy', '__doPostBack(\'" + getClientID() + @"\')')";
            imgCalendar.Attributes.Add("onclick", scriptStr);
        }

        // Get the id of the control rendered on client side
        // Very essential for Javascript Calendar scripts to locate the textbox
        public string getClientID()
        {
            return txt_Date.ID;
        }		
    }
}
